package com.cheng.im.gateway.processer;

import com.cheng.im.common.constants.ProtoInstants;
import com.cheng.im.common.bean.msg.ProtoMsgV1;
import com.cheng.im.common.constants.MQConstants;
import com.cheng.im.gateway.mq.RocketMQProduce;
import com.cheng.im.gateway.protobuilder.ChatMsgBuilder;
import com.cheng.im.gateway.protobuilder.ChatResponseBuilder;
import com.cheng.im.gateway.session.LocalSession;
import com.cheng.im.gateway.session.dao.impl.RedissonUserCacheImpl;
import com.cheng.im.service.common.eunms.ConversationTypeEnum;
import com.cheng.im.service.common.resp.CommonResult;
import com.cheng.im.service.message.service.CheckSendMessageService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Data
@Slf4j
@Service
public class ChatMsgProcessor {

    @DubboReference
    private CheckSendMessageService checkSendMessageService;
    @Resource
    ChatResponseBuilder chatResponseBuilder;
    @Resource
    ChatMsgBuilder chatMsgBuilder;
    @Resource
    RocketMQProduce rocketMQProduce;
    @Resource
    private RedissonUserCacheImpl userCacheDAO;

    public Boolean actionP2P(LocalSession fromSession, ProtoMsgV1.Message proto){
        // 聊天处理
        ProtoMsgV1.MessageRequest messageRequest = proto.getMessageRequest();
        // 检验是否可以发送
        CommonResult checkResult = checkSendMessageService
                .checkFriendShip(messageRequest.getFrom(), messageRequest.getTo(), proto.getAppId());
        if(!checkResult.isOk()){
            // 非好友关系不能发送
            ProtoMsgV1.Message message = chatResponseBuilder
                    .chatResponse(proto.getSequence(), ProtoInstants.ResultCodeEnum.NOT_ALLOW_SEND);

            fromSession.getChannel().writeAndFlush(message);
            return false;
        }

        rocketMQProduce.sendMessage(MQConstants.IM_2_MSG_SERVICE, proto);

        return true;
    }

    public Boolean actionP2G(LocalSession session, ProtoMsgV1.Message proto) {
        // 聊天处理
        ProtoMsgV1.MessageGroupRequest msg = proto.getMessageGroupRequest();

        // 检验是否可以发送
        CommonResult commonResult = checkSendMessageService
                .checkGroupMessage(msg.getFrom(), msg.getGroupId(), proto.getAppId());
        if(!commonResult.isOk()){
            // 不能发送
            ProtoMsgV1.Message message = chatResponseBuilder
                    .chatResponse(proto.getSequence(), ProtoInstants.ResultCodeEnum.NOT_ALLOW_SEND);

            session.getChannel().writeAndFlush(message);
            return false;
        }

        rocketMQProduce.sendMessage(MQConstants.IM_2_GROUP_MESSAGE_SERVICE, proto);
        return true;
    }

    public Boolean actionOther(LocalSession session, ProtoMsgV1.Message pkg) {
        // todo: 这边直接发了
        rocketMQProduce.sendMessage(MQConstants.IM_2_GROUP_MESSAGE_SERVICE, pkg);

        return true;
    }

    public Boolean actionReceiveAck(LocalSession session, ProtoMsgV1.Message pkg) {
        rocketMQProduce.sendMessage(MQConstants.IM_2_MSG_SERVICE, pkg);
        return true;
    }

    public Boolean actionMsgRead(LocalSession session, ProtoMsgV1.Message pkg) {

        if (pkg.getMessageRead().getConversationType() == ConversationTypeEnum.GROUP.getCode()) {
            rocketMQProduce.sendMessage(MQConstants.IM_2_GROUP_MESSAGE_SERVICE, pkg);
            return true;
        }
        rocketMQProduce.sendMessage(MQConstants.IM_2_MSG_SERVICE, pkg);
        return true;
    }
}
